<?php

namespace app\common\model;

use think\Model;

class DeviceMap extends Model
{
    //
    static function onlineDevice($user_id){
        $device_ids = Device::userDeviceId($user_id);
        $list = DeviceMap::all(function($query) use($device_ids){
            $query->where([
                "device_id"=>["IN",$device_ids]
            ]);
        });
        $ids = [];
        foreach($list as $item){
            $ids[] = $item->device_id;
        }
        return $ids;
    }



    static function bind_device_map($cid,$device_me){
        $device = Device::get(["device_me" => $device_me]);
        $d_m = new DeviceMap();
        $d_m_o = $d_m->where(["cid" => $cid])->find();
        if (!empty($device)) {//绑定设备
            if (empty($d_m_o)) {
                $d_m_o = new DeviceMap();
            }
            $d_m_o->device_id = $device->id;
            $d_m_o->cid = $cid;
            $d_m_o->save();
        }
    }
}
